package com.cc.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cc.entity.Advice;
import com.cc.entity.UserStudent;
import com.cc.service.AdviceService;
import com.cc.service.FnService;

@Controller  
@RequestMapping("/advice") 
public class AdviceController {
	@Resource  
	protected FnService fnService;
	@Resource  
	protected AdviceService adviceService;
	
	@ResponseBody
	@RequestMapping("/private/add")  
    public String add(HttpServletRequest request,Model model,Advice newadv){  
        UserStudent token=(UserStudent)request.getSession().getAttribute("token");
        UserStudent currentUser=fnService.getCurrentUserStudent(token);
		int state=adviceService.addAdvice(currentUser, newadv);
    	return state+"";  
    }
	
	@ResponseBody
	@RequestMapping("/private/list")  
    public String list(HttpServletRequest request,Model model,Advice newadv){  
        UserStudent token=(UserStudent)request.getSession().getAttribute("token");
        UserStudent currentUser=fnService.getCurrentUserStudent(token);
		List<Advice> data=adviceService.getAllAdvice(currentUser);
		ObjectMapper jsonmapper=new ObjectMapper(); 
		String result="";
		try {
			result=jsonmapper.writeValueAsString(data);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return result;  
    }
}
